home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / system_sdl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-20  |  4.7 KB  |  213 lines  |  [TEXT/CWIE]

  1. /* system specific functions (basically, an SDL wrapper) */
  2.  
  3. #include "system.h"
  4.  
  5. static int redisplay = 0;
  6. static callbacks *current = 0;
  7. static SDL_Surface *screen;
  8. static int width, height;
  9. static int flags;
  10. static int fullscreen;
  11. static int video_initialized = 0;
  12.  
  13. void SystemExit() {
  14.   fprintf(stderr, "shutting down sound now\n");
  15. #ifdef SOUND
  16.   shutdownSound();
  17. #endif
  18.   fprintf(stderr, "shutting down network now\n");
  19. #ifdef NETWORK
  20.   SystemNetExit();
  21. #endif
  22.   fprintf(stderr, "shutting down sdl now\n");
  23.   SDL_Quit();
  24.   fprintf(stderr, "exiting application\n");
  25.   exit(0);
  26. }
  27.  
  28. void SystemInit(int *argc, char *argv[]) {
  29.   if(SDL_Init(SDL_INIT_VIDEO) < 0 ){
  30.     fprintf(stderr, "Couldn't initialize SDL video: %s\n", SDL_GetError());
  31.     exit(2);
  32.   }
  33.   else video_initialized = 1;
  34.  
  35.   if(SDL_Init(SDL_INIT_AUDIO) < 0 ){
  36.     fprintf(stderr, "Couldn't initialize SDL audio: %s\n", SDL_GetError());
  37.   }
  38.   /* atexit(SystemExit); */
  39.  
  40.   SDL_EnableKeyRepeat(0, 0); /* turn keyrepeat off */
  41. #ifdef NETWORK
  42.   SystemNetInit();
  43. #endif
  44. }
  45.  
  46. void SystemGrabInput() {
  47.   SDL_WM_GrabInput(SDL_GRAB_ON);
  48. }
  49.  
  50. void SystemUngrabInput() {
  51.   SDL_WM_GrabInput(SDL_GRAB_OFF);
  52. }
  53.  
  54. void SystemPostRedisplay() {
  55.   redisplay = 1;
  56. }
  57.  
  58. int SystemGetElapsedTime() {
  59.   /* fprintf(stderr, "%d\n", SDL_GetTicks()); */
  60.   return SDL_GetTicks();
  61. }
  62.  
  63. void SystemSwapBuffers() {
  64.   SDL_GL_SwapBuffers();
  65. }
  66.  
  67. void SystemWarpPointer(int x, int y) {
  68.   SDL_WarpMouse(x, y);
  69. }
  70.  
  71. void SystemHidePointer() {
  72.   SDL_ShowCursor(0);
  73. }
  74.  
  75. void SystemUnhidePointer() {
  76.   SDL_ShowCursor(1);
  77. }
  78.  
  79. void SystemMouse(int buttons, int state, int x, int y) {
  80.   if(current)
  81.     if(current->mouse != NULL)
  82.       current->mouse(buttons, state, x, y);
  83. }
  84.  
  85. void SystemMouseMotion(int x, int y) {
  86.   if(current)
  87.     if(current->mouseMotion != NULL)
  88.       current->mouseMotion(x, y);
  89. }
  90.  
  91. void SystemMainLoop() {
  92.   SDL_Event event;
  93.   char *keyname;
  94.   char key;
  95.  
  96.   while(1) {
  97.     while(SDL_PollEvent(&event) && current) {
  98.       if(event.type == SDL_KEYDOWN) {
  99.     keyname = SDL_GetKeyName(event.key.keysym.sym);
  100.     key = 0;
  101.     switch(event.key.keysym.sym) {
  102.     case SDLK_SPACE: key = ' '; break;
  103.     case SDLK_ESCAPE: key = 27; break;
  104.     case SDLK_RETURN: key = 13; break;
  105.     default:
  106.       if(keyname[1] == 0) key = keyname[0];
  107.       break;
  108.     }
  109.     /* check: is that translation necessary? */
  110.     if(key) 
  111.       current->keyboard(key, 0, 0);
  112.     else
  113.       current->keyboard(event.key.keysym.sym, 0, 0);
  114.       } else if(event.type == SDL_MOUSEBUTTONDOWN ||
  115.         event.type == SDL_MOUSEBUTTONUP) {
  116.     SystemMouse(event.button.button, event.button.state, 
  117.             event.button.x, event.button.y);
  118.       } else if(event.type == SDL_MOUSEMOTION) {
  119.     SystemMouseMotion(event.motion.x, event.motion.y);
  120.       }
  121.     }
  122.     if(redisplay) {
  123.       current->display();
  124.       redisplay = 0;
  125.     } else
  126.       current->idle();
  127.   }
  128. }
  129.   
  130. void SystemRegisterCallbacks(callbacks *cb) {
  131.   current = cb;
  132. }
  133.  
  134. void SystemInitWindow(int x, int y, int w, int h) {
  135.   width = w;
  136.   height = h;
  137. }
  138.  
  139. void SystemInitDisplayMode(int f, unsigned char full) {
  140.   flags = f;
  141.   fullscreen = full;
  142.   if(!video_initialized) {
  143.     if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
  144.       fprintf(stderr, "can't initialize Video: %s\n", SDL_GetError());
  145.       exit(2);
  146.     }
  147.   } else {
  148.     fprintf(stderr, "WARNING: can't init video; it's already running\n");
  149.   }
  150.   if(flags & SYSTEM_DOUBLE)
  151.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
  152.   if(flags & SYSTEM_DEPTH)
  153.     SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
  154.   /* SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8); */
  155.   video_initialized = 1;
  156. }
  157.  
  158. int SystemCreateWindow(char *name) {
  159.   int f = SDL_OPENGL;
  160.   if(fullscreen & SYSTEM_FULLSCREEN)
  161.     f |= SDL_FULLSCREEN;
  162.   if( (screen = SDL_SetVideoMode( width, height, 0, f )) == NULL ) {
  163.     fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
  164.     exit(1);
  165.   }
  166.   else return 1;
  167. }
  168.  
  169. void SystemDestroyWindow(int id) {
  170.   // SDL_QuitSubSystem(SDL_INIT_VIDEO);
  171.   video_initialized = 0;
  172. }
  173.  
  174. void SystemReshapeFunc(void(*reshape)(int, int)) {
  175. }
  176.  
  177. extern char* SystemGetKeyName(int key) {
  178.   /*
  179.   char *buf;
  180.   buf = malloc(2);
  181.   buf[0] = (char)key;
  182.   buf[1] = 0;
  183.   return buf;
  184.   */
  185.   return SDL_GetKeyName(key);
  186. }  
  187.  
  188. int SystemWriteBMP(char *filename, int x, int y, unsigned char *pixels) {
  189.   /* this code is shamelessly stolen from Ray Kelm, but I believe he
  190.      put it in the public domain */
  191.   SDL_Surface *temp;
  192.   int i;
  193.  
  194.   temp = SDL_CreateRGBSurface(SDL_SWSURFACE, x, y, 24,
  195. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  196.                   0x000000FF, 0x0000FF00, 0x00FF0000, 0
  197. #else
  198.                   0x00FF0000, 0x0000FF00, 0x000000FF, 0
  199. #endif
  200.                   );
  201.  
  202.   if (temp == NULL)
  203.     return -1;
  204.  
  205.   for (i = 0; i < y; i++)
  206.     memcpy(((char *) temp->pixels) + temp->pitch * i, 
  207.        pixels + 3 * x * (y - i - 1), x * 3);
  208.  
  209.   SDL_SaveBMP(temp, filename);
  210.   SDL_FreeSurface(temp);
  211.   return 0;
  212. }
  213.